package miiteen.customsportal.Api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import miiteen.core.result.ResultMessage;
import miiteen.core.result.ResultMessageTool;
import miiteen.customsportal.ct.beans.CTNoticeType;
import miiteen.customsportal.ct.service.CTNoticeTypeService;
import miiteen.customsportal.data.beans.DataInfo;
import miiteen.customsportal.notice.beans.NoticeInfo;
import miiteen.customsportal.notice.service.NoticeInfoService;
import miiteen.customsportal.pubconfig.HgWebPubTable;
import miiteen.data.beans.PageBounds;
import miiteen.demo.utils.CommonUtil;
import miiteen.module.pubtable.beans.PubTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 兰关动态
 */
@Api(tags = "兰关动态")
@RestController
@RequestMapping("/Api/noticeInfoApi")
@CrossOrigin
public class NoticeInfoApiController {
    @Autowired
    private NoticeInfoService noticeInfoService;
    @Autowired
    private CTNoticeTypeService ctNoticeTypeService;

    @Autowired
    private CommonUtil commonUtil;
    //查询兰关动态类型
    @ApiOperation("查询动态类型")
    @RequestMapping(value = "/getLgType", method = RequestMethod.GET)
    public ResultMessage<Object> getLgType(@ApiParam(name = "codeValue", value = "编码") @RequestParam String codeValue){
        Map<String,Object> map=new HashMap<>();
        if(codeValue!=null&&!codeValue.equals("")&&!codeValue.trim().isEmpty()){
            map.put("codeValue",codeValue);
            commonUtil.handleLikeSearch(map,"codeValue");
        }
        List<CTNoticeType> codeValue1 = ctNoticeTypeService.getCodeValue(map);

        if(codeValue1!=null&&codeValue1.size()>0){
            return ResultMessageTool.Success("查询成功", codeValue1);
        }else{
            return ResultMessageTool.Success("查询失败");
        }
    }
    //根据动态类型查询动态信息
    @ApiOperation("根据动态类型查询动态信息")
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public ResultMessage<Object> findAll(@ApiParam(name = "noticeTypeId", value = "动态类型ID")@RequestParam String noticeTypeId,@RequestParam(value = "title",required = false) String title,@RequestParam  Integer start){
        Map<String,Object> map=new HashMap<>();
        Map<String,Object> resultMap=new HashMap<>();
        if(noticeTypeId!=null&&!noticeTypeId.equals("")&&!noticeTypeId.trim().isEmpty()){
            map.put("noticeTypeId",Long.valueOf(noticeTypeId));
        }
        map.put("isDisplay",1);
        map.put("order",1);

        if(!"".equals(title)){
            map.put("title",title);
        }
        List<NoticeInfo> count=noticeInfoService.findListByMap(map);
        int totalCount=count.size();
        PageBounds pageBounds = new PageBounds();
        List<NoticeInfo> list=new ArrayList<>();
        if (start > 0 ) {
            pageBounds.setPageSize(10);
            pageBounds.setPageNum(start);
            list= noticeInfoService.findListByMap(map,pageBounds);
        }else if(start==0){
            pageBounds.setPageSize(8);
            pageBounds.setPageNum(1);
            list= noticeInfoService.findListByMap(map,pageBounds);
        }else{
            list=noticeInfoService.findListByMap(map);
        }
        resultMap.put("infoList",list);
        resultMap.put("totalCount",totalCount);
        if(list!=null&&list.size()>0){
            return ResultMessageTool.Success("查询成功", resultMap);
        }else{
            return ResultMessageTool.Error("查询失败");
        }
    }





    //根据ID查询该条动态详情
    @ApiOperation("根据ID查询该条动态详情")
    @RequestMapping(value = "/getNoticeId", method = RequestMethod.GET)
    public ResultMessage<Object> getNoticeId(@ApiParam(name = "id", value = "该条记录ID") @RequestParam String id){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        Map<String,Object> resultMap=new HashMap<>();
        if(id!=null&&!id.equals("")&&!id.trim().isEmpty()){
            NoticeInfo noticeInfo = noticeInfoService.findById(Long.valueOf(id));
            if(noticeInfo!=null){
                resultMap.put("noticeInfo",noticeInfo);
                Map<String,Object> map=new HashMap<>();
                map.put("createTimePrevious",sdf.format(noticeInfo.getCreateTime()));
                List<NoticeInfo> Previous = noticeInfoService.findListByMapPreviousNext(map);
                if(Previous!=null&&Previous.size()>0){
                    resultMap.put("Previous",Previous.get(0));
                }
                map.clear();
                map.put("createTimeNext",sdf.format(noticeInfo.getCreateTime()));
                List<NoticeInfo> Next = noticeInfoService.findListByMapPreviousNext(map);
                if(Next!=null&&Next.size()>0){
                    resultMap.put("Next",Next.get(0));
                }

                return ResultMessageTool.Success("查询成功", resultMap);
            }else{
                return ResultMessageTool.Error("查询失败");
            }
        }
        return ResultMessageTool.Error("参数错误，查询失败");
    }
}
